﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;

namespace centraldba_dbrestore_utility.Data
{
    public class MyRestoreScript : INotifyPropertyChanged
    {
        private string _name, _command, _result;
        bool _succeeded, _processing, _started;

        #region " Events "

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        #region " Constructor "

        public MyRestoreScript(string name, string command)
        {
            _name = name;
            _command = command;
            _result = string.Empty;
            _started = false;
            _processing = false;
            _succeeded = true;
        }

        #endregion

        #region " Properties "

        public string Name
        {
            get { return _name; }
            set 
            {
                if (!value.Equals(_name))
                {
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        public string Command
        {
            get { return _command; }
            set
            {
                if (!value.Equals(_command))
                {
                    _command = value;
                    NotifyPropertyChanged("Command");
                }
            }
        }

        public string Result
        {
            get { return _result; }
            set
            {
                if (!value.Equals(_result))
                {
                    _result = value;
                    NotifyPropertyChanged("Result");
                }
            }
        }

        public bool Started
        {
            get { return _started; }
            set
            {
                if (!value.Equals(_started))
                {
                    _started = value;
                    NotifyPropertyChanged("Started");
                    NotifyPropertyChanged("Status");
                    NotifyPropertyChanged("StatusImage");
                }
            }
        }

        public bool Succeeded
        {
            get { return _succeeded; }
            set
            {
                if (!value.Equals(_succeeded))
                {
                    _succeeded = value;
                    NotifyPropertyChanged("Succeeded");
                    NotifyPropertyChanged("Status");
                    NotifyPropertyChanged("StatusImage");
                }
            }
        }

        public bool Processing
        {
            get { return _processing; }
            set
            {
                if (!value.Equals(_processing))
                {
                    _processing = value;
                    NotifyPropertyChanged("Processing");
                    NotifyPropertyChanged("Status");
                    NotifyPropertyChanged("StatusImage");
                }
            }
        }

        public string Status
        {
            get
            {
                // Load image from resources
                if (_processing)
                    return "Processing";
                else if (_succeeded && _started)
                    return "Succeeded";
                else if (_started)
                    return "Failed";
                else
                    return "";
            }
        }

        public BitmapImage StatusImage
        {
            get
            {
                // Create the image
                BitmapImage bmp = new BitmapImage();
                bmp.CacheOption = BitmapCacheOption.OnLoad;
                bmp.BeginInit();

                // Load image from resources
                if (_processing)
                    bmp.StreamSource = System.Windows.Application.GetResourceStream(new Uri("Images/wait_16trans.gif", UriKind.RelativeOrAbsolute)).Stream;
                else if (_succeeded && _started)
                    bmp.StreamSource = System.Windows.Application.GetResourceStream(new Uri("Images/success_16.png", UriKind.RelativeOrAbsolute)).Stream;
                else if (_started)
                    bmp.StreamSource = System.Windows.Application.GetResourceStream(new Uri("Images/failure_16.png", UriKind.RelativeOrAbsolute)).Stream;
                else
                    bmp.StreamSource = System.Windows.Application.GetResourceStream(new Uri("Images/Shim.png", UriKind.RelativeOrAbsolute)).Stream;
                bmp.EndInit();

                // Return the image
                return bmp;
            }
        }

        #endregion
    }
}
